package com.yx.shop.dao.admin.impl;

import com.yx.shop.dao.admin.AdminOrderDao;
import com.yx.shop.entitly.Orders;
import com.yx.shop.entitly.Product;
import com.yx.shop.entitly.Type;
import com.yx.shop.entitly.User;
import com.yx.shop.utils.JDBCUtils;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapListHandler;

import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Classname AdminOrderDaoImpl
 * @Description TODO:
 * @Date 2021/05/10 20:29
 * @Created by yx
 */
public class AdminOrderDaoImpl implements AdminOrderDao {
    private QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource());
    @Override
    public List<Orders> getAllOrder() {
        try {
            String sql = "SELECT oid,orders.uid AS uid,aid,ocount,otime,ostate,uname,upassword,uemail,usex,ustatus,ucode,urole FROM orders INNER JOIN USER ON orders.uid = user.uid;";
            List<Map<String, Object>> query = queryRunner.query(sql, new MapListHandler());
            if (query != null) {
                //保存数据
                List<Orders> ordersList = new ArrayList<>();
                for (Map<String, Object> stringObjectMap : query) {
                    Orders orders = new Orders();
                    User user = new User();
                    //封装数据
                    BeanUtils.populate(orders, stringObjectMap);
                    BeanUtils.populate(user, stringObjectMap);
                    orders.setUser(user);
                    ordersList.add(orders);
                }
                return ordersList;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return null;
    }
}
